//
//  LSMineViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/18.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSMineViewController.h"
#import "LSPersonalInfoViewController.h"
#import "LSMineTableViewCell.h"
#import "LSMineTableViewAddSectionHeaderCell.h"
#import "LSMinePersonalEntranceCell.h"// 个人信息控制器
#import "LSWaitForExamineViewController.h"// 待办事项控制器
#import "LSContactsViewController.h"// 通讯录控制器
#import "LSDownloadFilesViewController.h"//文件下载控制器
#import "LSExportFilesViewController.h"//导出文件控制器
#import "LSSettingsViewController.h"// 设置控制器
#import "LSLoginViewController.h"
#import "LSYiBanVC.h"
@interface LSMineViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *mineTableView;
@property (nonatomic, strong) NSArray <NSArray <NSDictionary *> *> *contentArray;
@property (nonatomic, assign) BOOL isSetContentOffset;

@property (nonatomic, strong) NSArray *ceshiArray;
@end

static NSString *LSMineTableViewCellID = @"LSMineTableViewCellID";
static NSString *LSMineTableViewAddSectionHeaderCellID = @"LSMineTableViewAddSectionHeaderCellID";
static NSString *LSMinePersonalEntranceCellID = @"LSMinePersonalEntranceCellID";

@implementation LSMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.ceshiArray = @[];
    if (![UserInfoModel sharedInstance].isLogin || [[UserInfoModel sharedInstance].customerId isEqualToString:@""]) [self presentLoginVC];
    self.isSetContentOffset = false;
    [self.view addSubview:self.mineTableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willGoLoginVC) name:UINotificationWillGoLoginMessageName object:nil];
}

- (void)presentLoginVC {
    LSLoginViewController *loginVC = [[LSLoginViewController alloc] init];
    HEBaseNavController *loginNav = [[HEBaseNavController alloc] initWithRootViewController:loginVC];
    loginNav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:loginNav animated:false completion:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getUserInfo];
}

- (void)getUserInfo {
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:UserInfo param:@{@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token]} completionHandler:^(id responseObject, NSError *connectionError) {
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            if (status.integerValue == 200) {
                NSArray *dataArray = [responseObject objectForKey:@"data"];
                if (dataArray && dataArray.count > 0) {
                    NSDictionary *dataDict = dataArray.firstObject;
                    if (dataDict && dataDict.count > 0) {
                        NSString *mobilephone = dataDict[@"mobilephone"];
                        if (mobilephone && ![mobilephone isEqualToString:@""]) {
                            [UserInfoModel sharedInstance].customerMobile = mobilephone;
                        } else {
                            [UserInfoModel sharedInstance].customerMobile = @"";
                        }
                        NSString *sex = dataDict[@"sex"];
                        if (sex && ![sex isEqualToString:@""]) {
                            [UserInfoModel sharedInstance].customerSex = sex;
                        } else {
                            [UserInfoModel sharedInstance].customerSex = @"0";
                        }
                        [UserInfoModel sharedInstance].customerRealName = [ValidateUtils validateNull:dataDict[@"realname"]];
                        [UserInfoModel sharedInstance].customerCategoryId = [ValidateUtils validateNull:dataDict[@"post"]];
                        [self.mineTableView reloadData];
                    }
                }
            } else {
                [MBProgressHUD showTipMessageInWindow:[ValidateUtils validateNull:responseObject[@"message"]]];
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
    }];
}

- (void)willGoLoginVC {
    self.tabBarController.selectedIndex = 0;
    [[NSNotificationCenter defaultCenter] postNotificationName:UINotificationGoLoginMessageName object:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.contentArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArray[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        LSMinePersonalEntranceCell *cell = [tableView dequeueReusableCellWithIdentifier:LSMinePersonalEntranceCellID forIndexPath:indexPath];
        cell.imageName = @"pic_tx";
        cell.userNickName = [ValidateUtils validateNull:[UserInfoModel sharedInstance].customerNickname];
        cell.userMobile = [ValidateUtils validateNull:[UserInfoModel sharedInstance].customerMobile];
        return cell;
    } else {
        if (indexPath.row == 0) {
            LSMineTableViewAddSectionHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:LSMineTableViewAddSectionHeaderCellID forIndexPath:indexPath];
            cell.imageName = self.contentArray[indexPath.section][indexPath.row][@"image"];
            cell.titleLabelName = self.contentArray[indexPath.section][indexPath.row][@"title"];
            return cell;
        } else {
            LSMineTableViewCell *mineCell = [tableView dequeueReusableCellWithIdentifier:LSMineTableViewCellID forIndexPath:indexPath];
            mineCell.imageName = self.contentArray[indexPath.section][indexPath.row][@"image"];
            mineCell.titleLabelName = self.contentArray[indexPath.section][indexPath.row][@"title"];
            return mineCell;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if (indexPath.section == 0) {
        //个人信息控制器
        LSPersonalInfoViewController *personalInfoVC = [[LSPersonalInfoViewController alloc] init];
        personalInfoVC.iconImage = [UIImage imageNamed:@"pic_tx"];
        [self.navigationController pushViewController:personalInfoVC animated:true];
    } else if (indexPath.section == 2) {
        //设置控制器
        LSSettingsViewController *settingsVC = [[LSSettingsViewController alloc] init];
        [self.navigationController pushViewController:settingsVC animated:true];
    } else {
        if (indexPath.row == 0) {
            LSYiBanVC *lvc = [[LSYiBanVC alloc]init];
            [self.navigationController pushViewController:lvc animated:YES];
        }else if (indexPath.row == 1) {
            //待办事项
            LSWaitForExamineViewController *waitForExamineVC = [[LSWaitForExamineViewController alloc] init];
            [self.navigationController pushViewController:waitForExamineVC animated:true];
        } else if (indexPath.row == 2) {
            // 通讯录
            LSContactsViewController *contactsVC = [[LSContactsViewController alloc] init];
            [self.navigationController pushViewController:contactsVC animated:true];
        } else if (indexPath.row == 3) {
            LSDownloadFilesViewController *downloadFilesVC = [[LSDownloadFilesViewController alloc] init];
            [self.navigationController pushViewController:downloadFilesVC animated:true];
        } else if (indexPath.row == 4) {
            LSExportFilesViewController *exportFilesVC = [[LSExportFilesViewController alloc] init];
            [self.navigationController pushViewController:exportFilesVC animated:true];
        }
//        if (indexPath.row == 0) {
//            //已办事项
//        } else if (indexPath.row == 1) {
//            //待办事项
//            LSWaitForExamineViewController *waitForExamineVC = [[LSWaitForExamineViewController alloc] init];
//            [self.navigationController pushViewController:waitForExamineVC animated:true];
//        } else if (indexPath.row == 2) {
//            // 通讯录
//            LSContactsViewController *contactsVC = [[LSContactsViewController alloc] init];
//            [self.navigationController pushViewController:contactsVC animated:true];
//        } else if (indexPath.row == 3) {
//            // 文件下载
//            LSContactsViewController *contactsVC = [[LSContactsViewController alloc] init];
//            [self.navigationController pushViewController:contactsVC animated:true];
//        } else {
//            // 待上传
//        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return LSMinePersonalEntranceCellHeight+15;
    } else {
        if (indexPath.row == 0) {
            return LSMineTableViewAddSectionHeaderCellHeight;
        } else {
            return LSMineTableViewCellHeight;
        }
    }
}

- (UITableView *)mineTableView {
    if (!_mineTableView) {
        _mineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight-SafeAreaBottomHeight-49)];
        _mineTableView.separatorInset = UIEdgeInsetsMake(0, 5, 0, 0);
        _mineTableView.backgroundColor = hexColor(EEEEEE);
        _mineTableView.delegate = self;
        _mineTableView.dataSource = self;
        _mineTableView.tableFooterView = [UIView new];
        [_mineTableView registerClass:LSMineTableViewCell.class forCellReuseIdentifier:LSMineTableViewCellID];
        [_mineTableView registerClass:LSMinePersonalEntranceCell.class forCellReuseIdentifier:LSMinePersonalEntranceCellID];
        [_mineTableView registerClass:LSMineTableViewAddSectionHeaderCell.class forCellReuseIdentifier:LSMineTableViewAddSectionHeaderCellID];
    }
    return _mineTableView;
}

- (NSArray<NSArray<NSDictionary *> *> *)contentArray {
    if (!_contentArray) {
        _contentArray = @[@[@{@"image":@"",@"title":@""}],
                          @[@{@"image":@"hetongyiban",@"title":@"已办事项"},@{@"image":@"dbsx",@"title":@"待办事项"},@{@"image":@"txl",@"title":@"通讯录"},@{@"image":@"wjxz",@"title":@"文件下载"},@{@"image":@"wjxz",@"title":@"导出文件"}],
                          @[@{@"image":@"shizhi",@"title":@"设置"}]];
    }
    return _contentArray;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
